package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.Information;
import com.example.entity.authority.Party;
import com.example.ext.entity.ExtInformation;
import com.example.mapp.InformationMapp;
import com.example.mapp.PartyMapp;
import com.example.mapp.PromotionMapp;
import com.example.service.InformationService;
import com.example.util.PromotionUtil;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2017/10/12.
 */
@Service
public class InformationServiceImpl implements InformationService{

    private final static Logger logger = (Logger) LoggerFactory.getLogger(InformationServiceImpl.class);

    @Autowired
    InformationMapp informationMapp;

    @Autowired
	private PromotionMapp promotionMapp;

    @Autowired
    private PartyMapp partyMapp;

    private static Map<String, Map<Long, Integer>> IMPORT_PARTY_MAP = new HashMap<>();
    
    @Override
    public Map<String, Object> queryList(ExtInformation extInformation) {
        List<ExtInformation> list =null;
        Map<String,Object> map = new HashMap();
        try {
//            Page<ExtInformation> page = PageHelper.startPage(extInformation.getCurrentPage(), extInformation.getPageSize());
            list = informationMapp.queryList(extInformation);
            int total = informationMapp.countList(extInformation);
            map.put("list",list);
            map.put("total",total);

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }

    @Override
    @Transactional
    public Map<String, Object> add(ExtInformation extInformation) {
        try{
            int i = informationMapp.add(extInformation);
            Party party = partyMapp.queryById(extInformation.getCreatePartyId() + "");

            String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
            Map<Long, Integer> tmpMap = IMPORT_PARTY_MAP.get(date);
            if (null == tmpMap) {
            	IMPORT_PARTY_MAP.clear();
            	tmpMap = new HashMap<>();
            	tmpMap.put(extInformation.getCreatePartyId(), 1);
            	PromotionUtil.calcParty(promotionMapp, 5, party.getPartyType(), extInformation.getCreatePartyId(), "上传1条资讯", "0");
            	IMPORT_PARTY_MAP.put(date, tmpMap);
            } else if (tmpMap.get(extInformation.getCreatePartyId())!=null &&
                    tmpMap.get(extInformation.getCreatePartyId()) <= 3) {
            	tmpMap.put(extInformation.getCreatePartyId(), tmpMap.get(extInformation.getCreatePartyId()) + 1);
            	PromotionUtil.calcParty(promotionMapp, 5, party.getPartyType(), extInformation.getCreatePartyId(), "上传1条资讯", "0");
            } else if (tmpMap.get(extInformation.getCreatePartyId())==null){
                tmpMap.put(extInformation.getCreatePartyId(), 1);
                PromotionUtil.calcParty(promotionMapp, 5, party.getPartyType(), extInformation.getCreatePartyId(), "上传1条资讯", "0");
            }
        }catch(Exception e){
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("新增失败",e);
            return ResultUtil.returnError("新增失败",null);
        }
        return ResultUtil.returnSuccess("新增成功",null);
    }

    @Override
    public Map<String, Object> delete(String id) {
        try{
            informationMapp.deleteById(id);

        }catch(Exception e){
            logger.error("删除失败",e);
            return ResultUtil.returnError("删除失败",null);
        }
        return ResultUtil.returnSuccess("删除成功",null);
    }

    @Override
    public Map<String, Object> update(ExtInformation extInformation) {
        try{
            informationMapp.update(extInformation);

        }catch(Exception e){
            logger.error("修改失败",e);
            return ResultUtil.returnError("修改失败",null);
        }
        return ResultUtil.returnSuccess("修改成功",null);
    }

    @Override
    public Map<String, Object> queryById(String id) {
        Information information = null;
        try{
            information = informationMapp.queryById(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败",null);
        }
        return ResultUtil.returnSuccess("查询成功",information);
    }
}
